/*
 * GDO GeometryFactory
 * 
 * initial implementation : 31.8.2007
 *
 * Copyright (c) Adam Sida, asida@centrum.cz.
 * All rights reserved.
 * 
 * for license see Petra.Gis.License.txt
 */
using System;
using System.Collections.Generic;
using System.Text;
using Petra.Gis.Geometry;
using Petra.Gis.Geometry.Support;

namespace Petra.Gis.DataServices.GDO
{
  public unsafe partial class GeometryFactory
  {
    /***********************************************
     * 28.8.2007
     * 
     * creates Label
     * from intergraph blob structure
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/
    private static GeometryBase binaryToLabel(byte[] vData, int arrayOffset, System.Type parentGeomType)
    {
      int lTextSize;
      Label oGeom = new Label();
      CoordinateList Coords = new CoordinateList(vData, arrayOffset + 16, 1, Dimension.dim3D);
      oGeom.coordList = Coords;

      //get rotation
      fixed (byte* bCoordListPtr = &vData[arrayOffset + 40])
      {
        double* dRotationPtr = (double*)bCoordListPtr;
        oGeom.Rotation = *dRotationPtr;
      }

      //text size
      fixed (byte* bCoordListPtr = &vData[arrayOffset + 76])
      {
        Int32* lIntPtr = (Int32*)bCoordListPtr;
        lTextSize = *lIntPtr;
      }
      //get normal vector
      oGeom.NormalVector = new NormalVector(ref vData,arrayOffset + 48);

      //get alignment (byte)
      oGeom.TextAlignment = (Label.TextAlignmentType)vData[arrayOffset + 75];

      //select encoding (byte)
      //const short gmfRichText = 0x00;
      //const short gmfUnformatted = 0x01; //.NET default
      //const short gmfUnicode = 0x02;
      System.Text.Encoding encoding;
      if (vData[arrayOffset + 74] == 1)
        encoding = System.Text.Encoding.Default;
      else
        encoding = System.Text.Encoding.Unicode;

      //get text
      oGeom.Text = encoding.GetString(vData, arrayOffset + 81, lTextSize);

      /*
      System.IO.MemoryStream tmpTxtStream = new System.IO.MemoryStream(vData);
      System.IO.BinaryReader oReader = new System.IO.BinaryReader(tmpTxtStream, System.Text.Encoding.Default);
      oReader.BaseStream.Position = 80;
      oReader.ReadChars(7);

      System.Text.Encoding encoding = System.Text.Encoding.UTF8;
      // Read string from binary file with UTF8 encoding
      byte[] buffer = new byte[30];
      binary_file.Read(buffer, 0, 30);
      return encoding.GetString(buffer);
      */

      return oGeom;
    }






    /***********************************************
     * 28.8.2007
     * 
     * creates intergraph blob structure 
     * from Point
     * 
     * asida@centrum.cz, initial implementation
     *************************************************/

    private static byte[] labelToBinary(GeometryBase geometry)
    {
      int lBlobSize;
      Guid gGeomGUID;

      Label _Label = (Label)geometry;

      lBlobSize = 16 + 24 + 8 + 24 + 4 + 4 + _Label.Text.Length;
      gGeomGUID = getGuid[GeometryType.Text];

      System.IO.MemoryStream oIMStream = new System.IO.MemoryStream(lBlobSize);
      System.IO.BinaryWriter mBWriter = new System.IO.BinaryWriter(oIMStream);

      oIMStream.Position = 0;
      mBWriter.Write(gGeomGUID.ToByteArray());

      mBWriter.Write(_Label.coordList.ToArray(Dimension.dim3D));
      mBWriter.Write(_Label.Rotation);

      //todo:fuck !! do something w/ this shit !!!
      mBWriter.Write((double)_Label.NormalVector.I);
      mBWriter.Write((double)_Label.NormalVector.J);
      mBWriter.Write((double)_Label.NormalVector.K);

      //2 bytes always 0
      mBWriter.Write((byte)0);
      mBWriter.Write((byte)0);

      mBWriter.Write((byte)_Label.TextEncoding);
      mBWriter.Write((byte)_Label.TextAlignment);

      //encode text
      //richText will be encoded unicode
      System.Text.Encoding encoding;
      if (_Label.TextEncoding == Label.TextEncodingType.WindowAnsi)
        encoding = System.Text.Encoding.Default;
      else
        encoding = System.Text.Encoding.Unicode;

      //get text as encoded bytes
      byte[] txtData = encoding.GetBytes(_Label.Text);

      mBWriter.Write((Int32)txtData.Length);
      mBWriter.Write(txtData);

      return oIMStream.ToArray();
    }
  }
}
